"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = _default;

var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));

var _assert = _interopRequireDefault(require("assert"));

var _pkg = _interopRequireDefault(require("@pxxtech/component/lib/pkg"));

var _Plugin = _interopRequireDefault(require("./Plugin"));

var defaultCustomName = function defaultCustomName(opts) {
  if (opts.customName) {
    return opts.customName;
  }

  return function (name) {
    if (_pkg.default.includes(name)) {
      return "@pxxtech/component/lib/".concat(name);
    }

    return "".concat(opts.libraryName, "/").concat(opts.libraryDirectory || 'lib', "/").concat(name);
  };
};

var defaultCustomStyleName = function defaultCustomStyleName(opts) {
  if (opts.customStyleName) {
    return opts.customStyleName;
  }

  return function (name) {
    if (_pkg.default.includes(name)) {
      return "@pxxtech/component/lib/".concat(name, "/style");
    }

    return "".concat(opts.libraryName, "/").concat(opts.libraryDirectory || 'lib', "/").concat(name, "/style");
  };
};

function _default(_ref) {
  var types = _ref.types;
  var plugins = null; // Only for test
  // eslint-disable-next-line no-underscore-dangle

  global.__clearBabelAntdPlugin = function () {
    plugins = null;
  };

  function applyInstance(method, args, context) {
    // eslint-disable-next-line no-restricted-syntax
    var _iteratorNormalCompletion = true;
    var _didIteratorError = false;
    var _iteratorError = undefined;

    try {
      for (var _iterator = plugins[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
        var plugin = _step.value;

        if (plugin[method]) {
          plugin[method].apply(plugin, [].concat((0, _toConsumableArray2.default)(args), [context]));
        }
      }
    } catch (err) {
      _didIteratorError = true;
      _iteratorError = err;
    } finally {
      try {
        if (!_iteratorNormalCompletion && _iterator.return != null) {
          _iterator.return();
        }
      } finally {
        if (_didIteratorError) {
          throw _iteratorError;
        }
      }
    }
  }

  var Program = {
    enter: function enter(path, _ref2) {
      var _ref2$opts = _ref2.opts,
          opts = _ref2$opts === void 0 ? {} : _ref2$opts;

      // Init plugin instances once.
      if (!plugins) {
        if (Array.isArray(opts)) {
          plugins = opts.map(function (opt, index) {
            var libraryName = opt.libraryName,
                libraryDirectory = opt.libraryDirectory,
                style = opt.style,
                styleLibraryDirectory = opt.styleLibraryDirectory,
                camel2DashComponentName = opt.camel2DashComponentName,
                camel2UnderlineComponentName = opt.camel2UnderlineComponentName,
                fileName = opt.fileName,
                transformToDefaultImport = opt.transformToDefaultImport;
            (0, _assert.default)(libraryName, 'libraryName should be provided');
            return new _Plugin.default(libraryName, libraryDirectory, style, styleLibraryDirectory, defaultCustomStyleName(opt), camel2DashComponentName, camel2UnderlineComponentName, fileName, defaultCustomName(opt), transformToDefaultImport, types, index);
          });
        } else {
          (0, _assert.default)(opts.libraryName, 'libraryName should be provided');
          plugins = [new _Plugin.default(opts.libraryName, opts.libraryDirectory, opts.style, opts.styleLibraryDirectory, defaultCustomStyleName(opts), opts.camel2DashComponentName, opts.camel2UnderlineComponentName, opts.fileName, defaultCustomName(opts), opts.transformToDefaultImport, types)];
        }
      }

      applyInstance('ProgramEnter', arguments, this); // eslint-disable-line
    },
    exit: function exit() {
      applyInstance('ProgramExit', arguments, this); // eslint-disable-line
    }
  };
  var methods = ['ImportDeclaration', 'CallExpression', 'MemberExpression', 'Property', 'VariableDeclarator', 'ArrayExpression', 'LogicalExpression', 'ConditionalExpression', 'IfStatement', 'ExpressionStatement', 'ReturnStatement', 'ExportDefaultDeclaration', 'BinaryExpression', 'NewExpression', 'ClassDeclaration', 'SwitchStatement', 'SwitchCase'];
  var ret = {
    visitor: {
      Program: Program
    }
  }; // eslint-disable-next-line no-restricted-syntax

  var _loop = function _loop() {
    var method = _methods[_i];

    ret.visitor[method] = function () {
      // eslint-disable-line
      applyInstance(method, arguments, ret.visitor); // eslint-disable-line
    };
  };

  for (var _i = 0, _methods = methods; _i < _methods.length; _i++) {
    _loop();
  }

  return ret;
}
